package springcloud.controller;

import com.atguigu.springcloud.message.InnerProtocol;
import com.atguigu.springcloud.message.MessageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.binding.BinderAwareChannelResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.UUID;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 直接配置到一个服务模块bus-sdk
 * @date 2021/10/11 16:49
 */
@EnableBinding
@RestController
public class DynamicDestinationController {

    @Autowired
    private BinderAwareChannelResolver resolver;

    @Autowired
    private MessageFactory messageFactory;


    @RequestMapping(path = "/{dest}", method = RequestMethod.POST, consumes = "*/*")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void handleRequest(@PathVariable("dest") String dest) {
        sendMessage(dest);
    }

    /**
     * @param dest 通道名
     * @return
     * @description: 向指定的通道发送消息
     */
    private void sendMessage(String dest) {
        String serial = UUID.randomUUID().toString();
        resolver.resolveDestination(dest).send(MessageBuilder.withPayload(serial).build());
    }


    /**
     * @param
     * @return
     * @description: 测试监听任务
     */
    @GetMapping("/message")
    public void tets() {
        String data = "发送数据啦";
        messageFactory.send("message-center", new InnerProtocol().body(data).cmd("/message"));

    }

}
